package com.dy.video.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.video.utlis.aliyun.ConstantVodUtils;
import com.dy.video.Video;
import com.dy.video.service.VideoService;
import com.dy.video.mapper.VideoMapper;
import entity.vo.videoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
* @author DELL
* @description 针对表【video】的数据库操作Service实现
* @createDate 2024-11-21 14:28:54
*/
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
    implements VideoService{

    @Override
    public String upload(MultipartFile file) {

        try {

            String fileName=file.getOriginalFilename();
            InputStream inputStream=file.getInputStream();
            String title=fileName.substring(0,fileName.lastIndexOf("."));
            //这五个参数 前两个是 id 密钥 第三个是上传到阿里云后的名称 第四个是文件名 第五个是文件流
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
            /* 存储区域(可选) */
            request.setStorageLocation("outin-9edd8647a88111efa0ee00163e0aeba8.oss-cn-beijing.aliyuncs.com");

            /* 点播服务接入点 */
            request.setApiRegionId("cn-beijing");

            UploadVideoImpl uploader = new UploadVideoImpl();

            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");


                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");

                videoId=response.getVideoId();
            }
            return videoId;
        }catch (Exception e){
            return null;
        }


    }
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<videoVo> findAll() {
        return videoMapper.findAll();
    }

    @Override
    public List<videoVo> findByVideo(Integer uid) {
        return videoMapper.findByVideo(uid);
    }

    @Override
    public void updateByComnum(Integer vid) {
        videoMapper.updateByComnum(vid);
    }

    @Override
    public List<videoVo> findAll1(Video v) {
        return videoMapper.findAll1(v);
    }

    @Override
    public List<videoVo> findAll2() {
        return videoMapper.findAll2();
    }

    @Override
    public List<Video> findList(Video video) {
        return  videoMapper.findList(video);
    }
}




